<?php
namespace org;

class Article {

    public $redis = null;

    //60*60*24/20=4320,每个点赞得到的分数，反之即之。
    public $score = 4320;

    //点赞增加数，或者点hate增加数
    public $num = 1;

    //init redis
    public $redis_host = "127.0.0.1";
    public $redis_port = "6379";
    public $redis_pass = "";

    public function __construct()
    {
        $this->redis = new \Redis();
        $this->redis->connect($this->redis_host,$this->redis_port);;
    }

    /**
     * @param int $user_id 用户id
     * @param int $type 点击的类型 1.点like，2.点hate
     * @param int $comment_id 文章id
     * @return string json;
     */
    public function click($user_id,$type,$comment_id)
    {
        //判断redis是否已经缓存了该文章数据
        //使用：分隔符对redis管理是友好的
        //这里使用redis zset-> zscore()方法
        if($this->redis->zscore("comment:like",$comment_id))
        {
            //已经存在

            //判断以前是否点过，点的是什么？
            //redis hash-> hget()
            $data = array();
            $rel = $this->redis->hget("comment:record",$user_id.":".$comment_id);

//            print_dump($rel);
            if(!$rel)
            {
                //什么都没点过
                //点赞加1
                $this->redis->zincrby("comment:like",$this->num,$comment_id);
                //增加分数
                $this->redis->zincrby("comment:score",$this->score,$comment_id);
                //记录上次操作
                $this->redis->hset("comment:record",$user_id.":".$comment_id,$type);

                $data = array(
                    "state" => 1,
                    "status" => 200,
                    "msg" => "like+1",
                );
            }
            else if($rel==$type)
            {
                //点赞减1
                $this->redis->zincrby("comment:like",-($this->num),$comment_id);
                //减掉分数
                $this->redis->zincrby("comment:score",-($this->score),$comment_id);
                $data = array(
                    "state" => 2,
                    "status" => 200,
                    "msg" => "like-1",
                );
            }


        }else{
            //点赞加一
            $this->redis->zincrby("comment:like",$this->num,$comment_id);
            //分数增加
            $this->redis->zincrby("comment:score",$this->score,$comment_id);
            $data = array(
                "state" => 7,
                "status" => 200,
                "msg" => "like+1",
            );
        }

        print_r($data);
        //判断是否需要更新数据
        $this->ifUploadList($comment_id);


    }

    public function ifUploadList($comment_id)
    {
        date_default_timezone_set("Asia/Shanghai");
        $time = strtotime(date('Y-m-d H:i:s'));

        if(!$this->redis->sismember("comment:uploadset",$comment_id))
        {
            //文章不存在集合里，需要更新
            $this->redis->sadd("comment:uploadset",$comment_id);
            //更新到队列
            $data = array(
                "id" => $comment_id,
                "time" => $time,
            );
            $json = json_encode($data);
            $this->redis->lpush("comment:uploadlist",$json);
        }
    }

}

